package com.mxx.common.netty;

import com.mxx.common.netty.http.NettyHttpHandler;
import com.mxx.common.netty.ws.NettyWebsocketConnectionForwardHandler;
import com.mxx.common.netty.ws.NettyWebsocketHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.logging.LoggingHandler;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/6/29 17:00
 * @Description: 暂无描述
 */
public class NettyWebsocketChannelInitializer extends ChannelInitializer<SocketChannel> {
    private NettyServiceProperties nettyServiceProperties;

    private NettyWebsocketHandler nettyWebSocketHandler;
    private NettyWebsocketConnectionForwardHandler connectionHttpHandler;
    private NettyHttpHandler nettyHttpHandler;

    public NettyWebsocketChannelInitializer(NettyWebsocketHandler nettyWebSocketHandler, NettyWebsocketConnectionForwardHandler connectionHttpHandler, NettyHttpHandler nettyHttpHandler, NettyServiceProperties nettyServiceProperties) {
        this.nettyWebSocketHandler = nettyWebSocketHandler;
        this.connectionHttpHandler = connectionHttpHandler;
        this.nettyHttpHandler = nettyHttpHandler;
        this.nettyServiceProperties = nettyServiceProperties;
    }

    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline().addLast("logging", new LoggingHandler());//设置log监听器，并且日志级别为debug，方便观察运行流程
        ch.pipeline().addLast("http-codec", new HttpServerCodec());//设置解码器
        ch.pipeline().addLast("aggregator", new HttpObjectAggregator(nettyServiceProperties.getMaxContextLength()));//聚合器，使用websocket会用到
//        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//用于大数据的分区传输
        ch.pipeline().addLast(connectionHttpHandler);//自定义的业务handler
        ch.pipeline().addLast(nettyWebSocketHandler);//自定义的业务handler
        ch.pipeline().addLast(nettyHttpHandler);//自定义的业务handler
    }
}
